import { clearItems, getToken } from "../utils/auth";
export default async function ResMethod(
  url: string,
  method: string,
  data: any | undefined = undefined,
) {

  if (method === "GET" && data) {
 
    data = new URLSearchParams(data)
  
    console.log(data)

    const getData = await fetch("http://127.0.0.1:8600" + url + `?${data}`, {
      method,
      headers: {
        "Content-Type": "application/json",
        "Authorization": getToken() || "",
      },
    })
    if (getData.ok){
      return getData.json()
    }
    else{
      if (getData.status===401) {
        clearItems()
        return 401
      }
    return false

  }}

  const res = await fetch("http://127.0.0.1:8600" + url, {
    method,
    headers: {
      "Content-Type": "application/json",
      "Authorization": getToken() || "",
    },
    body: JSON.stringify(data),
  })
  console.log(res)
  if (res.ok){
    return res.json()
  }
  else{
    if (res.status===401) {
      clearItems()
      return 401
    }
  return false

  



}}